package com.webwork.mood.record.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.webwork.mood.common.controller.SuperCtrl;
import com.webwork.mood.record.dataobject.MoodRecord;
import com.webwork.mood.record.service.MoodService;
import com.webwork.mood.system.dataobject.User;
import com.webwork.mood.system.service.UserService;
import com.webwork.mood.util.Paging;
import com.webwork.mood.util.StrManager;

@Controller
@RequestMapping(value = "/record")
public class RecordController extends SuperCtrl {

	@Autowired
	private UserService userSve;
	
	@Autowired
	private MoodService moodSve;
	
	@RequestMapping(value = "/record")
	@Override
	public String mainPage() {
		return "record";
	}
	
	@RequestMapping(value = "queryRecordList")
	@ResponseBody
	public ResponseEntity<String> queryList(
			@RequestParam(value = "page") int page,
			@RequestParam(value = "rows") int rows,
			@RequestParam(required = false, value = "userName") String userName)
					throws Exception {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Map<String, Object> ret = new HashMap<String, Object>();

		User queryObj = new User();
		if (!StrManager.isEmpty(userName)) {
			queryObj.setUserName(userName);
		}
		List<User> users = userSve.query(queryObj);
		if (users != null && users.size() > 0) {
			MoodRecord recordQuery = new MoodRecord();
			recordQuery.setUserId(users.get(0).getUserId());
			ret.put("total", moodSve.count(recordQuery));
			List<MoodRecord> list = moodSve.query(recordQuery, new Paging(page, rows));
			for (MoodRecord record : list) {
				if (!StrManager.isEmpty(userName)) {
					record.setUserId(userName);
				}
			}
			ret.put("rows", list);
		} else {
			ret.put("total", 0);
			ret.put("rows", new ArrayList<MoodRecord>());
		}
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value="getMoodScoreTable")
	@ResponseBody
	public ResponseEntity<String> getMoodScoreTable(
			@RequestParam(value="startTime") long startTime,
			@RequestParam(value="endTime") long endTime) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Map<String, Object> ret = new HashMap<String, Object>();
		
		ret.put("total", 1);
		ret.put("rows", moodSve.getMoodScoreTable(startTime, endTime));	//时间段内总人次   平均分
		
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value="getMoodScoreLine")
	@ResponseBody
	public ResponseEntity<String> getMoodScoreLine(
			@RequestParam(value="startTime") long startTime,
			@RequestParam(value="endTime") long endTime) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Map<String, Object> ret = new HashMap<String, Object>();
		
		ret.put("total", 1);
		ret.put("rows", moodSve.getMoodScoreLine(startTime, endTime));
		
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
}
